import { storeToRefs } from "pinia";
import { userStore } from "../store";
import { treeToList } from '../utils';

export default {
	async created(el, binding, vnode) {

	},

	async mounted(el, bindling) {
		const store = userStore();
		const { info } = storeToRefs(store);
		if (bindling.value instanceof Array) {
			//manager: admin|member
			const [manager, key, auth] = bindling.value;
			const modules = info.value.roles.map((d) => d.auth.map((d) => typeof key === 'number' ? d.key : d.name)).flat();
			if (!modules.includes(key) || auth.id !== info.value.id) {
				el.disabled = true;
				el.style.display = 'none';
			}
		} else {
			if (!info.value.is_supplier) {
				const modules = info.value.roles.map((d) => d.auth.map((d) => d.key)).flat();
				if (!modules.some(d => {
					return d === parseInt(bindling.value);
				})) {
					if (bindling.arg === 'show') {
						el.disabled = true;
						el.style.cursor = 'not-allowed';
					} else {
						el.disabled = true;
						el.style.display = 'none';
					}
				}
			} else {
				const modules = treeToList(JSON.parse(JSON.stringify(info.value.modules)));
				if (!modules.some(d => {
					return d.id === parseInt(bindling.value);
				})) {
					if (bindling.arg === 'show') {
						el.disabled = true;
						el.style.cursor = 'not-allowed';
					} else {
						el.disabled = true;
						el.style.display = 'none';
					}

				}
			}

		}
	},

	unmounted(el, binding, vnode) {
	}
}